
using System.Threading;
using System.Diagnostics;
using log4net;


namespace logger{
    class MutilThreadMutilFile{
        private long cnt_max_ = 0;
        private long tc_max_=0;
        private long tc_min_=9999;
        private long tc_all_=0;
        private long tc_all_real_=0;
        private long tc_5_=0;  // 1~5
        private long tc_10_=0; // 5~10
        private long tc_20_=0;  // 10~20
        private long tc_50_=0;  // 20~50
        private long tc_g50_=0;
        private long data_cnt_=0;
        private long[] data_arr_;

        public void test(){
            // Init
            Thread t1 = new Thread(new ThreadStart(threader2));
            Thread t2 = new Thread(new ThreadStart(threader1));
            Thread t3 = new Thread(new ThreadStart(threader3));
            cnt_max_ = 500000; 
            data_arr_ = new long[cnt_max_];
            // Launch
            t1.Start();
            t2.Start();
            t3.Start();
            // Wait for thread
            t1.Join();
            t2.Join();
            t3.Join();
            //
            show();
            System.Console.ReadKey();
        }

        private void threader1(){
            Stopwatch sw = new Stopwatch();
            Stopwatch sw_all = Stopwatch.StartNew();
            for(int i=0; i<cnt_max_; i++){
                string msg = string.Format("{0}",i);
                CANBoardLog.writeLog(msg);
                EngineCurrentLog.writeLog(msg);
                sw.Restart();
                RFIDLog.writeLog(msg);
                sw.Stop();

                update(sw);
                // Tip Message
                if (i%10000==0){
                    System.Console.WriteLine("Process {0}/{1}",i,cnt_max_);
                }
            }
            sw_all.Stop();
            tc_all_real_ = sw_all.ElapsedMilliseconds;
 
        }
        private void threader2(){
            Stopwatch sw = new Stopwatch();
            for(int i=0; i<cnt_max_; i++){
                string msg = string.Format("{0}",i);
                CANBoardLog.writeLog(msg);
                EngineCurrentLog.writeLog(msg);
                sw.Restart();
                RFIDLog.writeLog(msg);
                sw.Stop();

                update(sw);
            }
        }
        private void threader3(){
            Stopwatch sw = new Stopwatch();
            for(int i=0; i<cnt_max_; i++){
                string msg = string.Format("{0}",i);
                CANBoardLog.writeLog(msg);
                EngineCurrentLog.writeLog(msg);
                sw.Restart();
                RFIDLog.writeLog(msg);
                sw.Stop();

                update(sw);
            }
        }


        private void update(Stopwatch sw){
            long cur_tc = sw.ElapsedMilliseconds;
            lock(this){
                tc_all_+=cur_tc;

                if(tc_max_<cur_tc && data_cnt_>0) tc_max_ = cur_tc;
                if(tc_min_>cur_tc && data_cnt_>0) tc_min_ = cur_tc;
                if(cur_tc>50) tc_g50_++;    // 50+
                else if(cur_tc>20) tc_50_++; // 20~50
                else if(cur_tc>10) tc_20_++;  // 10~20
                else if(cur_tc>5) tc_10_++;   // 5~10   
                else if(cur_tc>1)  tc_5_++;  // 1~5

                if(cur_tc>0 && data_cnt_<cnt_max_) {
                    data_arr_[data_cnt_] = cur_tc;
                    data_cnt_++;
                }
            }
        }

        private void show(){
            string summary = string.Format("Count(All) = {0}\r\n"
                                        +"Time Consumption(Real All) = {1}\r\n"
                                        +"Time Consumption(All) = {2}\r\n"
                                        +"Time Consumption(Max) = {3}\r\n"
                                        +"Time Consumption(Min) = {4}\r\n"
                                        +"Time Consumption(1,5) = {5}\r\n"
                                        +"Time Consumption(5,10) = {6}\r\n"
                                        +"Time Consumption(10,20) = {7}\r\n"
                                        +"Time Consumption(20,50) = {8}\r\n"
                                        +"Time Consumption(50+) = {9}\r\n"
                                        ,cnt_max_,tc_all_real_,tc_all_,tc_max_
                                        ,tc_min_,tc_5_,tc_10_,tc_20_,tc_50_,tc_g50_);
            System.Console.WriteLine(summary);
            // Show Detail
            int show_step = 40;
            for(int i=0; i<cnt_max_+show_step; i+=show_step){
                string line = i+">"; 
                for(int j=i; j<i+show_step && j<cnt_max_; j++){
                    line=line+" "+data_arr_[j];
                }
                System.Console.WriteLine(line);
                System.Console.ReadKey();
            }
        }
    }
}